package cn.biplam.back.operator.ddl.sql2md;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Insert;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.interfaces.DataOperator;
import cn.biplam.common.opcode.sqlengine.DDLParserCode.FkTabColBufCode;
import cn.biplam.common.struc.MdStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;

/**
 * 源数据表外键字段关系缓冲区数据操作类 
 * <li>  插入数源数据表字段外键关系到缓存区
 * @author XiaoRuxing 2009-6-26下午09:01:51
 * @version 1.0
 */
public class FkTableColBuffer extends DataOperator{
	
	//302204
	public FkTableColBuffer() throws ErrorMessage{
		super("table_col_fk");
	}

	@Override
	protected OperatorResult _exchange() throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		  if(this.opId.equals(FkTabColBufCode.Insert)){
			this.insert(sql, filter);
			return this.doing(sql, Operator_Type.DML);
		}else if(this.opId.equals(FkTabColBufCode.Get_By_ID)){
			this.getRefById(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else if(this.opId.equals(FkTabColBufCode.Get_By_TaskID_And_ReId)){
			this.getRefByTaskIdAndReId(sql, filter);
			return this.doing(sql, Operator_Type.Select);
		}else{
			throw new ErrorMessage("Data Operator id not found:"+ opId+" !");
		}
	}

	/**
	 * 插入数源数据表字段外键关系到缓存区<br>
	 * 更新外键字段表的外键标志位1<br>
	 * @throws ErrorMessage 
	 */
	public void insert(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		String rcoid=generatorRowID();
		Insert insert = new Insert(this.tableName);
		insert.putStringFieldDenyNull(MdStruc.FkColBufferStruc.Fk_Column_Relation_ID, rcoid);
		insert.putStringFieldDenyNull(MdStruc.FkColBufferStruc.Fk_Relation_ID, filter.getValue(MdStruc.FkColBufferStruc.Fk_Relation_ID));
		insert.putStringFieldIgnoreNull(MdStruc.FkColBufferStruc.Main_Table_Column_ID, filter.getValue(MdStruc.FkColBufferStruc.Main_Table_Column_ID));
		insert.putStringFieldIgnoreNull(MdStruc.FkColBufferStruc.Foreign_Table_Column_ID, filter.getValue(MdStruc.FkColBufferStruc.Foreign_Table_Column_ID));
		insert.putStringFieldIgnoreNull(MdStruc.FkColBufferStruc.BATCH, filter.getValue(MdStruc.FkColBufferStruc.BATCH));
		sql.add(insert.getSimpleSql());
		
		/** 更新外键字段表的外键标志位1 */
		TableColBuffer tcb = new TableColBuffer();
		filter.put(MdStruc.SrcTabColBufStruc.COLUMN_NAME, filter.getValue(MdStruc.FkColBufferStruc.Foreign_Table_Column_ID));
		filter.put(MdStruc.SrcTabColBufStruc.BATCH, filter.getValue(MdStruc.FkColBufferStruc.BATCH));
		tcb.updateFkFlag(sql, filter);
	}
	
	/**
	 * 根据ID查询表关联字段信息 
	 * @throws ErrorMessage 
	 */
	public void getRefById(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp=new StringBuilder();
		sp.append("select * from "+this.tableName+"              ");
		sp.append("where rcoid='" +filter.getValue(MdStruc.FkColBufferStruc.Fk_Column_Relation_ID)+"' ");
		sp.append(" and "+MdStruc.FkColBufferStruc.BATCH+"='"+filter.getValue(MdStruc.FkColBufferStruc.BATCH)+"'");
		sql.add(sp.toString());
	}
	
	/**
	 * 根据任务ID和表关系id查询表关联字段信息 
	 * @throws ErrorMessage 
	 */
	private void getRefByTaskIdAndReId(ArrayList<String> sql,DataFilter filter) throws ErrorMessage{
		_init(filter);
		StringBuilder sp=new StringBuilder();
		sp.append("select * from "+this.tableName+"              ");
		sp.append("where reid='" +filter.getValue(MdStruc.FkColBufferStruc.Fk_Relation_ID)+"' ");
		sp.append(" and "+MdStruc.FkColBufferStruc.BATCH+"='"+filter.getValue(MdStruc.FkColBufferStruc.BATCH)+"'");
		sql.add(sp.toString());
	}
	
	/**
	 * 根据模型类型初始化表名  
	 */
	private void _init(DataFilter filter) throws ErrorMessage{
		String modelType = filter.getValue(MdStruc.FkColBufferStruc.MODEL_TYPE);
		if(TOOLS.StringIsEmpty(modelType)){
			throw new ErrorMessage("Model type is null");
		}
		this.tableName = "src_table_col_fk";
	}
}
